/**
 * @Author       : oak-c
 * @Date         : 2021-01-21 15:04:25
 * @Description  :
 * @LastEditors  : oak-c
 * @LastEditTime : 2021-01-21 15:49:19
 * @FilePath     : /src/service/sms/sms-ali.service.ts
 */

import { ISmsCodeOption } from '@/interface'
import { ConfigService } from '@donews/nestjs-config'
import { Injectable } from '@nestjs/common'
import Core from '@alicloud/pop-core'
import { CoreException, CoreExceptionType, CustomLoggerService, SmsExceptionType } from '@/modules'

interface AliSmsOp {
    RegionId: string
    PhoneNumbers: string
    SignName: string
    TemplateCode: string
    TemplateParam: string
}

@Injectable()
export class SmsAliService {
    constructor(private readonly config: ConfigService, private readonly logger: CustomLoggerService) {}

    private readonly smsOption = this.config.get('sms', null)

    /**
     * @description: 发送验证码
     * @param {ISmsCodeOption} option
     * @param {string} code
     * @return {*}
     */
    public async sendCode(option: ISmsCodeOption, code: string) {
        return await this.send({
            RegionId: 'cn-hangzhou',
            PhoneNumbers: option.phone,
            SignName: this.smsOption.sign.code.SignName,
            TemplateCode: this.smsOption.sign.code.TemplateCode,
            TemplateParam: JSON.stringify({ code }),
        })
    }

    /**
     * @description: 发送短信
     * @param {AliSmsOp} params
     * @return {*}
     */
    public async send(params: AliSmsOp) {
        const phoneReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[026789])\d{8}$/
        if (!params.PhoneNumbers || !phoneReg.test(params.PhoneNumbers)) {
            throw new CoreException(SmsExceptionType.ERROR_SMS_PHONE_FAIL)
        }
        const { Code, RequestId, BizId } = await new Core(this.smsOption.ali).request('SendSms', params, {
            method: 'POST',
        })
        if (Code === 'OK') {
            return {
                phone: params.PhoneNumbers,
                provider: 'ali',
                sgin: params.SignName,
                template: params.TemplateCode,
                content: params.TemplateParam,
                bizid: BizId,
                requestid: RequestId,
            }
        }
        switch (Code) {
            case 'isv.SMS_TEMPLATE_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_TEMPLATE_ILLEGAL)
            case 'isv.SMS_SIGNATURE_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_SIGNATURE_ILLEGAL)
            case 'isv.MOBILE_NUMBER_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_MOBILE_NUMBER_ILLEGAL)
            case 'isv.TEMPLATE_MISSING_PARAMETERS':
                throw new CoreException(SmsExceptionType.ERROR_SMS_TEMPLATE_MISSING_PARAMETERS)
            case 'isv.EXTEND_CODE_ERROR':
                throw new CoreException(SmsExceptionType.ERROR_SMS_EXTEND_CODE_ERROR)
            case 'isv.DOMESTIC_NUMBER_NOT_SUPPORTED':
                throw new CoreException(SmsExceptionType.ERROR_SMS_DOMESTIC_NUMBER_NOT_SUPPORTED)
            case 'isv.DAY_LIMIT_CONTROL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_DAY_LIMIT_CONTROL)
            case 'isv.SMS_CONTENT_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_CONTENT_ILLEGAL)
            case 'isv.SMS_SIGN_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_SIGN_ILLEGAL)
            case 'isv.RAM_PERMISSION_DENY':
                throw new CoreException(SmsExceptionType.ERROR_SMS_RAM_PERMISSION_DENY)
            case 'isv.OUT_OF_SERVICE':
                throw new CoreException(SmsExceptionType.ERROR_SMS_OUT_OF_SERVICE)
            case 'isv.PRODUCT_UN_SUBSCRIPT':
                throw new CoreException(SmsExceptionType.ERROR_SMS_PRODUCT_UN_SUBSCRIPT)
            case 'isv.PRODUCT_UNSUBSCRIBE':
                throw new CoreException(SmsExceptionType.ERROR_SMS_PRODUCT_UNSUBSCRIBE)
            case 'isv.ACCOUNT_NOT_EXISTS':
                throw new CoreException(SmsExceptionType.ERROR_SMS_ACCOUNT_NOT_EXISTS)
            case 'isv.ACCOUNT_ABNORMAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_ACCOUNT_ABNORMAL)
            case 'isv.INVALID_PARAMETERS':
                throw new CoreException(SmsExceptionType.ERROR_SMS_INVALID_PARAMETERS)
            case 'isv.SYSTEM_ERROR':
                throw new CoreException(SmsExceptionType.ERROR_SMS_SYSTEM_ERROR)
            case 'isv.INVALID_JSON_PARAM':
                throw new CoreException(SmsExceptionType.ERROR_SMS_INVALID_JSON_PARAM)
            case 'isv.BLACK_KEY_CONTROL_LIMIT':
                throw new CoreException(SmsExceptionType.ERROR_SMS_BLACK_KEY_CONTROL_LIMIT)
            case 'isv.PARAM_LENGTH_LIMIT':
                throw new CoreException(SmsExceptionType.ERROR_SMS_PARAM_LENGTH_LIMIT)
            case 'isv.PARAM_NOT_SUPPORT_URL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_PARAM_NOT_SUPPORT_URL)
            case 'isv.AMOUNT_NOT_ENOUGH':
                throw new CoreException(SmsExceptionType.ERROR_SMS_AMOUNT_NOT_ENOUGH)
            case 'isv.TEMPLATE_PARAMS_ILLEGAL':
                throw new CoreException(SmsExceptionType.ERROR_SMS_TEMPLATE_PARAMS_ILLEGAL)
            default:
                this.logger.error(`[SmsAliService] - send - ${JSON.stringify({ Code, RequestId, BizId })}`)
                throw new CoreException(CoreExceptionType.UNKNOWN_ERROR)
        }
    }
}
